package com.usian.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.constant.UserConstants;
import com.usian.model.article.pojo.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ArticleFeign;
import com.usian.user.feign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.AuthMapper;
import com.usian.user.service.AuthService;
//import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper, ApUserRealname>implements AuthService {

    @Resource
    private ApUserMapper apUserMapper;

    @Autowired
    private WemediaFeign wemediaFeign;

    @Autowired
    private ArticleFeign articleFeign;
    @Override
    public ResponseResult loadListByStatus(AuthDto dto) {
        //1、检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、检查分页参数
        dto.checkParam();
        //3、创建条件
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus()!=null){
            queryWrapper.eq(dto.getStatus()!=null,ApUserRealname::getStatus,dto.getStatus());
        }
        //4、进行查询
        Page page=new Page(dto.getPage(),dto.getSize());
        IPage iPage = page(page, queryWrapper);
        //5、封装参数
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal());
        //6、取出分页数据，存放result中
        result.setData(iPage.getRecords());

        return result;
    }
//@GlobalTransactional
    @Override
    public ResponseResult updateStatusById(AuthDto dto, Integer status) {
        //1、检查参数
        if (dto==null||dto.getId()==null){
             return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2、检查状态
        if (checkStatus((status))){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //3、修改ap_user_realname  状态
            //3.1创建ApUserRealName 对象 用来保存需要修改中传递的参数
        ApUserRealname userRealname = new ApUserRealname();

            //用户的ID和状态设置进来
        userRealname.setId(dto.getId());
        userRealname.setStatus(status);

            //3.2判定dto中的审核信息
        if (dto.getMsg()!=null){
            userRealname.setReason(dto.getMsg());
        }
            //3.3 更新状态
        updateById(userRealname);

        //4、添加自媒体人的账号和添加作者信息
        if (status.equals(UserConstants.PASS_AUTH)){
            ResponseResult result = createWemediaAndAuthor(dto);
            if (result!=null){
                return result;
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    //添加自媒体7账号和作家号
    public ResponseResult createWemediaAndAuthor(AuthDto dto){
        //4、添加自媒体人的账号
        //4.1 查询ApUserRealName信息 根据dto中提供的信息来进行查询
        ApUserRealname userRealname = getById(dto.getId());
        //4.2 根据userId 查询ApUser的相关数据
        ApUser apUser = apUserMapper.selectById(userRealname.getUserId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //4.3 根据ApUser中的用户名查询自媒体账号是否存在
        WmUser wmUser = wemediaFeign.findByName(apUser.getName());
        if (wmUser==null){
            //4.4 添加自媒体账号
            System.out.println(apUser.getId()+"8---------888888888888888888888888");
            wmUser = new WmUser();
            wmUser.setApUserId(apUser.getId());

            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setStatus(9);
            wmUser.setPhone(apUser.getPhone());
            wemediaFeign.save(wmUser);
        }
        //5、添加作者
            createAuthor(wmUser);
        return null;
    }
    //添加作家号
    public void createAuthor(WmUser wmUser){
        //1、根据userid 查询当前用户是否有作者号
        ApAuthor apAuthor = articleFeign.findByUserId(wmUser.getApUserId());
        if (apAuthor==null){
            //创建作家号
            ApAuthor author = new ApAuthor();
            author.setName(wmUser.getName());
            author.setType(UserConstants.AUTHOR_TYPE);
            author.setCreatedTime(new Date());
            author.setUserId(wmUser.getApUserId());
            articleFeign.save(author);
        }
    }
    /**
     * 状态检查
     * @param status
     * @return
     */
    public boolean checkStatus(Integer status){
        //status不能为null,status不能是审核成功9或失败2
        if (status!=null){
            return false;
        }
        return true;
    }
}
